using System.Windows;
using System.Windows.Controls;

namespace WPFPractice.UserControls.DynamicColumnTable
{
	/// <summary>
	/// 自动表格单元格模板选择器
	/// </summary>
	public class TableCellTemplateSelector : DataTemplateSelector
	{
		/// <summary>
		/// 选择模板
		/// </summary>
		/// <param name="item">单元格</param>
		/// <param name="container">容器</param>
		/// <returns></returns>
		public override DataTemplate SelectTemplate(object item, DependencyObject container)
		{
			var cell = item as TableCell;
			var fe = container as FrameworkElement;
			DataTemplate dt = null;
			if(cell != null && fe != null)
			{
				string typeName = $"{cell.Type}";
				if(cell.Type == TableCellTypeEnum.Float)
				{
					typeName = TableCellTypeEnum.Double.ToString();
				}

				typeName += "Type";
				dt = fe.FindResource(typeName) as DataTemplate;
			}

			return dt;
		}
	}
}
